// subPages/integral/submitOrders/submitOrders.js
// subPages/health/submitOrders/submitOrders.js
const app = getApp();
var util =  require('../../../utils/util.js');
var isBuy = false
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      })
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          })
        }
      }
    }
    if(options.list && options.num){
      var list = []
      list.push(JSON.parse(options.list))
      // console.log(list)
      that.setData({
        shopList: list,
        buyNum: options.num?options.num:1,
        total: Number(list[0].jf)*Number(options.num?options.num:1)
      })
    }
  },

  //获取积分
  getIntegral:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_user_info',function(res){
      // console.log(res)
      if(res.data){
        that.setData({
          integral: res.data.jf
        })
      }
    },{user_id: that.data.userId})
  },

  //获取地址数据
  getAddressData:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/get_user_address',function(res){
      // console.log(res)
      if(res.data.length!=0){
        that.setData({
          isAddress: true,
          addressList: res.data[0],
        })
      }else{
        that.setData({
          isAddress: false,
          addressList: [],
        })
        util.showModelAll('提示','请添加收货地址',function(){
          wx.navigateTo({
            url: '/subPages/mine/newAddress/newAddress?isOne=' + 1,
          })
        })
      }
    },{type:1,user_id: that.data.userId})
  },

  //地址管理
  addressBtn:function(){
    wx.navigateTo({
      url: '/subPages/mine/address/address',
    })
  },

  //新增地址管理
  newAddressBtn:function(){
    wx.navigateTo({
      url: '/subPages/mine/newAddress/newAddress?isOne=' + 1,
    })
  },

  //购买
  buyBtn:function(){
    var that = this
    if(isBuy){
      return
    }
    isBuy = true
    if(that.data.integral < that.data.total){
      util.showModal('积分提示','您当前积分不足，无法兑换')
      return
    }
    if(that.data.isAddress){
      var addressList = that.data.addressList
      if(addressList){
        var shopList = that.data.shopList
        var data = {
          "product_id": shopList[0].id,
          "community_id": that.data.communityId,
          "user_id": that.data.userId, 
          "one_jf": shopList[0].jf,
          "all_jf": that.data.total,
          "num": that.data.buyNum,
          "address": addressList.address,
          "sheng": addressList.sheng,
          "shi": addressList.shi,
          "qu": addressList.qu,
          "name": addressList.name,
          "phone": addressList.phone,
        }
        data = JSON.stringify(data)
        console.log(data)
        util.showModelAll('提示','是否确认兑换该商品',function(){
          util.postAjax(app.globalData.apiUrl +'/user_pay_jf_shop_product',function(res){
            console.log(res)
            if(res.data == 1){
              util.showToast('兑换成功','success',2000)
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1, // 回退前 delta(默认为1) 页面
                })
              },500)
            }else{
              util.showToast('兑换失败，请重新兑换','none',2000)
            }
            isBuy = false
          },{pro: data})
        },function(){
          isBuy = false
        })
      }else{
        util.showToast('加载失败，请重试','none',1000)
        isBuy = false
      }
    }else{
      isBuy = false
      util.showModelAll('提示','请添加收货地址',function(){
        wx.navigateTo({
          url: '/subPages/mine/newAddress/newAddress?isOne=' + 1,
        })
      })
    }
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getAddressData()
      that.getIntegral()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getAddressData()
          that.getIntegral()
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
